package kr.hkit.BusinessManagement.BasicDataMgnUI;

import javax.swing.JPanel;

import java.awt.BorderLayout;
import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import javax.swing.DefaultComboBoxModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import kr.hkit.BusinessManagement.DBCon.BusinessEmpDBCon;
import kr.hkit.BusinessManagement.DTO.BusinessEmpDTO;

public class PanelBusinessEmpMgn extends JPanel implements ActionListener {
	private JTextField tfBECode;
	private JTextField tfBEName;
	private JLabel lblBECode;
	private JLabel lblBEName;
	private JLabel lblBELevel;
	private JComboBox cbBELevel;
	private JButton btnSave;
	private JButton btnDelete;
	private JButton btnSearch;

	BusinessEmpDBCon empDB = new BusinessEmpDBCon();
	/**
	 * Create the panel.
	 */
	public PanelBusinessEmpMgn() {
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		add(panel, BorderLayout.SOUTH);
		
		btnSave = new JButton("저장");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				BusinessEmpDTO emp = getText();
				empDB.AddBusinessEmployee(emp);
			}
		});
		panel.add(btnSave);
		
		btnDelete = new JButton("삭제");
		btnDelete.addActionListener(this);
		panel.add(btnDelete);
		
		btnSearch = new JButton("검색");
		btnSearch.addActionListener(this);
		panel.add(btnSearch);
		
		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new GridLayout(0, 2, 0, 0));
		
		lblBECode = new JLabel("사원코드");
		lblBECode.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblBECode);
		
		tfBECode = new JTextField();
		panel_1.add(tfBECode);
		tfBECode.setColumns(10);
		
		lblBEName = new JLabel("사원명");
		lblBEName.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblBEName);
		
		tfBEName = new JTextField();
		panel_1.add(tfBEName);
		tfBEName.setColumns(10);
		
		lblBELevel = new JLabel("사원등급");
		lblBELevel.setHorizontalAlignment(SwingConstants.CENTER);
		panel_1.add(lblBELevel);
		
		cbBELevel = new JComboBox();
		cbBELevel.setModel(new DefaultComboBoxModel(new String[] {"A", "B", "C"}));
		panel_1.add(cbBELevel);

	}

	public BusinessEmpDTO getText(){
		
		String BECode = tfBECode.getText().toString().trim();
		String BEName = tfBEName.getText().toString().trim();
		String BELevel = getLevelFromCb(cbBELevel.getSelectedIndex());
		
		return new BusinessEmpDTO( BECode, BEName, BELevel);
		
	}
	
	public String getLevelFromCb(int cbNum){
		if(cbNum == 0){
			return "A";
		}else if(cbNum == 1){
			return "B";
		}else{
			return "C";
		}
	}
	
	public int putLevelIntoCb(String level){
		if(level.equals("A")){
			return 0;
		}else if(level.equals("B")){
			return 1;
		}else{
			return 2;
		}
	}
	
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == btnDelete) {
			actionPerformedBtnDelete(arg0);
		}
		if (arg0.getSource() == btnSearch) {
			actionPerformedBtnSearch(arg0);
		}
	}
	protected void actionPerformedBtnSearch(ActionEvent arg0) {
		String code = tfBECode.getText();
		BusinessEmpDTO emp = new BusinessEmpDTO();
		emp = empDB.searchEmpByCode(code);
		
		tfBECode.setText(emp.getCode());
		tfBEName.setText(emp.getName());
		cbBELevel.setSelectedIndex(putLevelIntoCb(emp.getLevel()));
		
	}
	protected void actionPerformedBtnDelete(ActionEvent arg0) {
		BusinessEmpDTO emp = new BusinessEmpDTO();
		emp = getText();
		empDB.deleteBusinessEmp(emp);
	}
}
